﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GovernmentOfficeProject.LevelDataAccessModel;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using GovernmentOfficeProject.SysTools;

namespace GovernmentOfficeProject.LevelDataAccess
{
    public class DAL_USER_User_Log
    {

        public Entity_USER_User_Log getUserLogByULID(decimal ulID)
        {
            try
            {               
                StringBuilder sbrSQL = new StringBuilder();
                sbrSQL.Append("select ULLOG.*,");
                sbrSQL.Append("ULFROMUSER.UserID as FromUserID,");
                sbrSQL.Append("ULFROMUSER.UserLoginName as FromUserLoginName,");
                sbrSQL.Append("ULFROMUSER.UserRealName as FromUserRealName,");
                sbrSQL.Append("ULTOUSER.UserID as ToUserID,");                
                sbrSQL.Append("ULTOUSER.UserLoginName as ToUserLoginName,");
                sbrSQL.Append("ULTOUSER.UserRealName as ToUserRealName,");
                sbrSQL.Append("ULMENU.MenuID,ULMENU.MenuName,");
                sbrSQL.Append("ULBUTTON.BtnID,ULBUTTON.BtnName");
                sbrSQL.Append(" from [PER_USER_LOG] as ULLOG");
                sbrSQL.Append(",[PER_USER] as ULFROMUSER,");
                sbrSQL.Append("[PER_USER] as ULTOUSER,");
                sbrSQL.Append("[SYS_MENU] as ULMENU,");
                sbrSQL.Append("[SYS_BUTTON] as ULBUTTON");
                sbrSQL.Append(" where ULLOG.ULID=@ulID");   //ulID在此.
                sbrSQL.Append(" and ULLOG.UL_From_UserID=ULFROMUSER.UserID");
                sbrSQL.Append(" and ULLOG.UL_To_UserID=ULTOUSER.UserID");
                sbrSQL.Append(" and ULLOG.UL_MenuID=ULMENU.MenuID");
                sbrSQL.Append(" and ULLOG.UL_ButtonID=ULBUTTON.BtnID");
                
                SqlParameter[] values = new SqlParameter[]{
                    new SqlParameter("@ulID",ulID)
                };
                AppLog.Write("DAL_USER_User_Log.getUserLogByULID--SQL:::" + sbrSQL.ToString(), AppLog.LogMessageType.Info);
                DataSet dst = DAL_DBGM.GetDataSet(sbrSQL.ToString(), values);
                Entity_USER_User_Log userLog = null;
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    userLog = new Entity_USER_User_Log();
                    userLog.UlID = Convert.ToDecimal(row["ULID"]);
                    userLog.FromUserID = Convert.ToDecimal(row["FromUserID"]);
                    userLog.ToUserID = Convert.ToDecimal(row["ToUserID"]);
                    userLog.MenuID = Convert.ToDecimal(row["MenuID"]);
                    userLog.ButtoonID = Convert.ToDecimal(row["BtnID"]);
                    userLog.ActiveTime  = Convert.ToString(row["UL_Active_Time"]);
                    userLog.ActiveStatus = Convert.ToDecimal(row["UL_Active_Status"]);
                    userLog.LogType = Convert.ToString(row["UL_LogType"]);
                    userLog.LogTypeValues = Convert.ToString(row["UL_LogTypeValues"]);
                    userLog.Backup1 = Convert.ToString(row["UL_Backup1"]);
                    userLog.Backup2 = Convert.ToString(row["UL_Backup2"]);
                    userLog.Backup3 = Convert.ToString(row["UL_Backup3"]);
                    userLog.Backup4 = Convert.ToString(row["UL_Backup4"]);
                    userLog.Backup5 = Convert.ToString(row["UL_Backup5"]);
                }
                return userLog;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_User_Log.getUserLogByULID(decimal ulID)---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }
        
        public List<Entity_USER_User_Log> getUserLogList()
        {
            try
            {
                StringBuilder sbrSQL = new StringBuilder();
                sbrSQL.Append("select ULLOG.*,");
                sbrSQL.Append("ULFROMUSER.UserID as FromUserID,");
                sbrSQL.Append("ULFROMUSER.UserLoginName as FromUserLoginName,");
                sbrSQL.Append("ULFROMUSER.UserRealName as FromUserRealName,");
                sbrSQL.Append("ULTOUSER.UserID as ToUserID,");
                sbrSQL.Append("ULTOUSER.UserLoginName as ToUserLoginName,");
                sbrSQL.Append("ULTOUSER.UserRealName as ToUserRealName,");
                sbrSQL.Append("ULMENU.MenuID,ULMENU.MenuName,");
                sbrSQL.Append("ULBUTTON.BtnID,ULBUTTON.BtnName");
                sbrSQL.Append(" from [PER_USER_LOG] as ULLOG");
                sbrSQL.Append(",[PER_USER] as ULFROMUSER,");
                sbrSQL.Append("[PER_USER] as ULTOUSER,");
                sbrSQL.Append("[SYS_MENU] as ULMENU,");
                sbrSQL.Append("[SYS_BUTTON] as ULBUTTON");

                AppLog.Write("DAL_USER_User_Log.getUserLogByULID--SQL:::" + sbrSQL.ToString(), AppLog.LogMessageType.Info);
                DataSet dst = DAL_DBGM.GetDataSet(sbrSQL.ToString());
                Entity_USER_User_Log userLog = null;
                List<Entity_USER_User_Log> userLogList = new List<Entity_USER_User_Log>();
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    userLog = new Entity_USER_User_Log();
                    userLog.UlID = Convert.ToDecimal(row["ULID"]);
                    userLog.FromUserID = Convert.ToDecimal(row["FromUserID"]);
                    userLog.ToUserID = Convert.ToDecimal(row["ToUserID"]);
                    userLog.MenuID = Convert.ToDecimal(row["MenuID"]);
                    userLog.ButtoonID = Convert.ToDecimal(row["BtnID"]);
                    userLog.ActiveTime = Convert.ToString(row["UL_Active_Time"]);
                    userLog.ActiveStatus = Convert.ToDecimal(row["UL_Active_Status"]);
                    userLog.LogType = Convert.ToString(row["UL_LogType"]);
                    userLog.LogTypeValues = Convert.ToString(row["UL_LogTypeValues"]);
                    userLog.Backup1 = Convert.ToString(row["UL_Backup1"]);
                    userLog.Backup2 = Convert.ToString(row["UL_Backup2"]);
                    userLog.Backup3 = Convert.ToString(row["UL_Backup3"]);
                    userLog.Backup4 = Convert.ToString(row["UL_Backup4"]);
                    userLog.Backup5 = Convert.ToString(row["UL_Backup5"]);
                    userLogList.Add(userLog);
                }
                return userLogList;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_User_Log.getUserLogList()---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }
        
        //留位: 多条件的日志查询,暂不需要.
        //public List<Entity_USER_User_Log> getUserLogListByEntity(Entity_USER_User_Log userLogEntity){}
        
        public bool addUserLog(Entity_USER_User_Log userLogEntity)
        {
            try
            {
                bool flag = false;
                bool blState = false;
                StringBuilder sqlBudIns = new StringBuilder();
                StringBuilder sqlBudValue = new StringBuilder();
                sqlBudIns.Append("insert into [PER_USER_LOG](");
                sqlBudValue.Append(" values(");
                List<SqlParameter> sprList = new List<SqlParameter>();
                if (userLogEntity.FromUserID != -1)
                {
                    sqlBudIns.Append("UL_FROM_UserID,");
                    sqlBudValue.Append("@fromUserID,");
                    sprList.Add(new SqlParameter("@fromUserID", userLogEntity.FromUserID));
                    blState = true;

                }
                if (userLogEntity.ToUserID != -1)
                {
                    sqlBudIns.Append("UL_To_UserID,");
                    sqlBudValue.Append("@toUserID,");
                    sprList.Add(new SqlParameter("@toUserID", userLogEntity.ToUserID));
                    blState = true;
                }
                if (userLogEntity.MenuID != -1)
                {
                    sqlBudIns.Append("UL_MenuID,");
                    sqlBudValue.Append("@menuID,");
                    sprList.Add(new SqlParameter("@menuID", userLogEntity.MenuID));
                    blState = true;
                }
                if (userLogEntity.ButtoonID != -1)
                {
                    sqlBudIns.Append("UL_ButtonID,");
                    sqlBudValue.Append("@buttonID,");
                    sprList.Add(new SqlParameter("@buttonID", userLogEntity.ButtoonID));
                    blState = true;
                }
                if (userLogEntity.ActiveTime!="" && userLogEntity.ActiveTime!=null)
                {
                    sqlBudIns.Append("UL_Active_Time,");
                    sqlBudValue.Append("@activeTime,");
                    sprList.Add(new SqlParameter("@activeTime", userLogEntity.ActiveTime));
                    blState = true;
                }
                if (userLogEntity.ActiveStatus != -1)
                {
                    sqlBudIns.Append("UL_Active_Status,");
                    sqlBudValue.Append("@activeStatus,");
                    sprList.Add(new SqlParameter("@activeStatus", userLogEntity.ActiveStatus));
                    blState = true;
                }
                if (userLogEntity.LogType!="" && userLogEntity.LogType!=null)
                {
                    sqlBudIns.Append("UL_LogType,");
                    sqlBudValue.Append("@logType,");
                    sprList.Add(new SqlParameter("@logType", userLogEntity.LogType));
                    blState = true;
                }
                if (userLogEntity.LogTypeValues!="" && userLogEntity.LogTypeValues!=null)
                {
                    sqlBudIns.Append("TL_LogTypeValues,");
                    sqlBudValue.Append("@logTypeValues,");
                    sprList.Add(new SqlParameter("@logTypeValues", userLogEntity.LogTypeValues));
                    blState = true;
                }
                if (userLogEntity.Backup1!="" && userLogEntity.Backup1!=null)
                {
                    sqlBudIns.Append("UL_Backup1,");
                    sqlBudValue.Append("@backup1,");
                    sprList.Add(new SqlParameter("@backup1", userLogEntity.Backup1));
                    blState = true;
                }
                if (userLogEntity.Backup2 != "" && userLogEntity.Backup2 != null)
                {
                    sqlBudIns.Append("UL_Backup2,");
                    sqlBudValue.Append("@backup2,");
                    sprList.Add(new SqlParameter("@backup2", userLogEntity.Backup2));
                    blState = true;
                }
                if (userLogEntity.Backup3 != "" && userLogEntity.Backup3 != null)
                {
                    sqlBudIns.Append("UL_Backup3,");
                    sqlBudValue.Append("@backup3,");
                    sprList.Add(new SqlParameter("@backup3", userLogEntity.Backup3));
                    blState = true;
                }
                if (userLogEntity.Backup4 != "" && userLogEntity.Backup4 != null)
                {
                    sqlBudIns.Append("UL_Backup4,");
                    sqlBudValue.Append("@backup4,");
                    sprList.Add(new SqlParameter("@backup4", userLogEntity.Backup4));
                    blState = true;
                }
                if (userLogEntity.Backup5 != "" && userLogEntity.Backup5 != null)
                {
                    sqlBudIns.Append("UL_Backup5,");
                    sqlBudValue.Append("@backup5,");
                    sprList.Add(new SqlParameter("@backup5", userLogEntity.Backup5));
                    blState = true;
                }

                if (blState)
                {
                    string strSQL = sqlBudIns.ToString().Substring(0, sqlBudIns.Length - 1) + ")" + sqlBudValue.ToString().Substring(0, sqlBudValue.Length - 1) + ")";
                    SqlParameter[] values = sprList.ToArray();
                    AppLog.Write("DAL-增加用户日志的SQL语句: " + strSQL, AppLog.LogMessageType.Debug);
                    flag = DAL_DBGM.InsertInfo(strSQL, values);
                }
                else
                {
                    //添加用户失败:没有任何数据.
                    AppLog.Write("添加失败:没有赋值任何数据.", AppLog.LogMessageType.Info);
                }
                return flag;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_User_Log.addUserLog(Entity_USER_User_Log userLogEntity)---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }


        //留位: 修改用户日志:暂不需要.


        public bool deleteUserLogByULID(decimal ulID)
        {
            try
            {
                string strSQL = "delete from [PER_USER_LOG] where ULID=@ulID";
                SqlParameter[] values = new SqlParameter[]{
                    new SqlParameter("@ulID",ulID)
                };
                return DAL_DBGM.DeleteInfo(strSQL,values);
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_User_Log.deleteUserLogByULID(decimal ulID)---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }

        }

        public bool deleteAllUserLog()
        {
            try
            {
                string strSQL = "delete from [PER_USER_LOG]";
                return DAL_DBGM.DeleteInfo(strSQL);
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_User_Log.deleteAllUserLog()---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }

        }
    }
}
